package com.farmproduct.controller;

import com.farmproduct.pojo.BackMes;
import com.farmproduct.pojo.SellUser;
import com.farmproduct.service.SellUserService;
import com.farmproduct.tools.SnowflakeIdWorker;
import com.farmproduct.vo.LoginRequestVO;
import com.farmproduct.vo.PasswordForm;
import com.farmproduct.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/selluser")
@CrossOrigin
public class SellUserController {
    @Autowired
    private SellUserService sellUserService;

    @RequestMapping(value = "/login")
    public ResultVO login(@RequestBody LoginRequestVO loginRequestVO){
        if (sellUserService.login(loginRequestVO)!=null){
            return new ResultVO(200,"success",0,sellUserService.login(loginRequestVO));
        }
        return new ResultVO();
    }

    //    添加用户
    @RequestMapping(value = "/add")
    public ResultVO addSellUser(@RequestBody SellUser sellUser){
        sellUser.setUid(SnowflakeIdWorker.generateId().toString());
        if (sellUserService.addSellUser(sellUser)){
            return new ResultVO(200,"success",0,null);
        }
        return new ResultVO();
    }



    @RequestMapping(value = "/update")
    public ResultVO updateSellUser(@RequestBody SellUser sellUser){
        if (sellUserService.updateSellUser(sellUser)){
            return new ResultVO(200,"success",0,null);
        }
        return new ResultVO();
    }

    @RequestMapping(value = "/updatepassword")
    public ResultVO updateSellUserPassword(@RequestBody PasswordForm passwordForm){
        if (sellUserService.updateSellUserPassword(passwordForm)){
            return new ResultVO(200,"success",0,null);
        }
        return new ResultVO();
    }

    @RequestMapping(value = "/getById/{uid}")
    public ResultVO getSellUserByid(@PathVariable String uid){
        SellUser userById = sellUserService.getUserById(uid);
        if (userById!=null){
            userById.setPassword("");
            return new ResultVO(200,"success",0,userById);
        }
        return new ResultVO();
    }
}

